#import <QuartzCore/QuartzCore.h>
#import <Foundation/Foundation.h>
#import "TTTableMyNeedsItemCell.h"
#import "TTTableMyNeedsItem.h"
#import "INDefaultStylesheet.h"
///////////////////////////////////////////////////////////////////////////////////////////////////

//static CGFloat kHPadding = 10;
static CGFloat kVPadding = 5;
static CGFloat kImageWidth = 47;
static CGFloat kImageHeight = 47;
static const CGFloat kSmallMargin = 6;
static const CGFloat kKeyWidth = 75;
static const CGFloat kKeySpacing = 12;


static const NSInteger kMessageTextLineCount = 0;
static const NSInteger kSubjectLineCount = 0;
static const CGFloat kMaxLabelHeight = 260;
static const CGFloat kMaxCharsLabel = 140;




///////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////
//////   TTTableMyNeedsItemCell     ////
//////////////////////////////////////////////////////////////

@implementation TTTableMyNeedsItemCell

+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)object {
	TTTableMyNeedsItem* item = object;
	
	CGFloat width = tableView.width - kImageWidth - 10;
	
//	CGSize textSize = [item.caption sizeWithFont:TTSTYLEVAR(needSubjectFont)
//							   constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
//								   lineBreakMode:UILineBreakModeWordWrap];
	
//	item.text = @"Dude";
	if ([item.text length] > kMaxCharsLabel) {
		item.text = [item.text substringWithRange:NSMakeRange(0, kMaxCharsLabel)];
		item.text = [item.text stringByAppendingString:@" ..."];
	}
	
	CGSize detailTextSize = [item.text sizeWithFont:TTSTYLEVAR(needTextFont)
								  constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
									  lineBreakMode:UILineBreakModeWordWrap];

	return detailTextSize.height + kVPadding*2 + kSmallMargin + kImageHeight ;	
		
}

//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
//{
//	TTLOG(@"Seems something is here");
//}


//-(UITableViewCellEditingStyle)tableView:(UITableView ;*)tableView editingStyleForRowAtIndexPath (NSIndexPath *)indexPath;




- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
	[super setHighlighted:highlighted animated:animated];
	if (highlighted) {
//		self.captionLabel.highlighted = TRUE;
//		self.detailTextLabel.highlighted = TRUE;
//		self.titleLabel.highlighted = TRUE;
//		self.timestampLabel.highlighted = TRUE;
//		_cityLabel.highlighted = TRUE;
//		self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);		
		self.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
    }
    else {
//		self.captionLabel.highlighted = FALSE;
//		self.detailTextLabel.highlighted = FALSE;
//		self.titleLabel.highlighted = FALSE;
//		self.timestampLabel.highlighted = FALSE;
//		_cityLabel.highlighted = FALSE;		
//		self.selectedBackgroundView.backgroundColor = [UIColor whiteColor];		
		self.backgroundColor = [UIColor clearColor];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
		[super setSelected:selected animated:animated];	
    // Configure the view for the selected state
//	if (selected) {
//		self.backgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
//		self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
//		self.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
//    }
//    else {
//		self.backgroundView.backgroundColor = [UIColor whiteColor];
//		self.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
//		self.backgroundColor = [UIColor clearColor];
//    }
}

//- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//	super setEditing:editing animated:animated];
//	//[self setNeedsLayout];
//
//	if (editing) {
//		TTLOG(@"We want to edit...");	
//		[UIView beginAnimations:nil context:nil];
//		[UIView setAnimationBeginsFromCurrentState:YES];
//		
//		//[super layoutSubviews];
//		
//		if (((UITableView *)self.superview).isEditing)
//		{
//			CGRect contentFrame = self.contentView.frame;
//			contentFrame.origin.x = 35;
//			self.contentView.backgroundColor = [UIColor grayColor];
//			self.contentView.frame = contentFrame;
//		}
//		else
//		{
//			CGRect contentFrame = self.contentView.frame;
//			contentFrame.origin.x = 0;
//			self.contentView.frame = contentFrame;
//		}
//		
//		[UIView commitAnimations];
//	}
//	else {
//		//clearSelectionForTableView
//		[super setEditing:NO animated:YES];
////		 [self.tableView setEditing:NO animated:YES];
//		
////		TTLOG(@"nada");
//	}
//
//
//}


///////////////////////////////////////////////////////////////////////////////////////////////////
//
//- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)identifier {
//	if (self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]) {
//		_item = nil;
//		
//		_imageView1 = [[TTImageView alloc] initWithFrame:CGRectZero];
//		[self.contentView addSubview:_imageView1];
//		
//	}
//	return self;
//}
//
//- (void)dealloc {
//	TT_RELEASE_SAFELY(_imageView1);
//	[super dealloc];
//}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)identifier {
	if (self = [super initWithStyle:style reuseIdentifier:identifier]) {
		self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
		self.titleLabel.contentMode = UIViewContentModeTopLeft;
		self.titleLabel.numberOfLines = 0;
		self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
		
		_cityLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		[self.contentView addSubview:_cityLabel];
		_helpsLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
		[self.contentView addSubview:_helpsLabel];		
		//		[self.contentView addSubview:_imageView1];
		

	}
	return self;
}

//- (void)dealloc {
//	TT_RELEASE_SAFELY(_cityLabel);
//	TT_RELEASE_SAFELY(_helpsLabel);	
//	[super dealloc];
//}

///////////////////////////////////////////////////////////////////////////////////////////////////
// UIView

- (void)layoutSubviews {
	[super layoutSubviews];	
	
//	self.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];	
	self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-shadow2.png"]] autorelease];
	//self.backgroundColor = [UIColor clearColor];
	
//	self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-shadow.png"]] autorelease];
	self.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.contentView.height)] autorelease];
//	self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
	self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
	self.selectedBackgroundView.frame = CGRectMake(0, 0, 320, self.contentView.height);

	
	CGFloat left = 4;
	CGFloat right = 15;
	CGFloat top = 4;
//	CGFloat height = self.contentView.height;
	CGFloat width = self.contentView.width - left;
	
	
	//Avatar
	//Round Corners to the image
	CALayer * imglayer = [self.imageView2 layer];
	[imglayer setMasksToBounds:YES];
	[imglayer setCornerRadius:5.0];
	[imglayer setBorderWidth:0.5];
	[imglayer setBorderColor:[TTSTYLEVAR(needPictureUrlBorderColor) CGColor]];
	
	self.imageView2.frame = CGRectMake(8, 8 , kImageWidth, kImageHeight);
	
	//Label DisplayName	
	[self.captionLabel sizeToFit];
	//	self.captionLabel.top = 10;
	//self.captionLabel.backgroundColor = [UIColor greenColor];
	
	if (self.captionLabel.text.length) {
		self.captionLabel.frame = CGRectMake(left + kImageWidth + kSmallMargin*2, top +kSmallMargin , width, self.captionLabel.font.lineHeight);
		//top += self.captionLabel.height;
	} else {
		self.captionLabel.frame = CGRectZero;
	}	
	
	//Label TimeStamp
	if (self.timestampLabel.text.length) {
		self.timestampLabel.alpha = !self.showingDeleteConfirmation;
		[self.timestampLabel sizeToFit];
		self.timestampLabel.left = self.contentView.width + kSmallMargin*2 - self.timestampLabel.width - 20;
		self.timestampLabel.top = top;
		self.titleLabel.width -=  self.timestampLabel.width + kSmallMargin*2;
	} else {
		self.timestampLabel.frame = CGRectZero;
	}
	self.timestampLabel.backgroundColor = [UIColor clearColor];
	
	//Label City
	if (_cityLabel.text) {
		_cityLabel.frame = CGRectMake(self.contentView.width - _cityLabel.width - kSmallMargin - 2, self.timestampLabel.bottom, 80, 10);
//		top += _cityLabel.height;
	} else {
		_cityLabel.frame = CGRectZero;
	}	
	
	//Label Helps
//	helpIcon.frame = CGRectMake(200, 8 , 16, 16);
	if (_helpsLabel.text) {
		_helpsLabel.frame = CGRectMake(self.timestampLabel.left - kSmallMargin - _helpsLabel.width, 1, 16 + 13 , 19);
		UIImageView *helpIcon = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comment.png"]] autorelease];
		helpIcon.top += 3; 
//		helpIcon.left +=10;
		[_helpsLabel addSubview:helpIcon];
	} else {
		_helpsLabel.frame = CGRectZero;
	}
	
	self.backgroundView.frame = CGRectMake(0,  self.contentView.height - 19, 320, 20);
	
	
	//Label Subject
	self.titleLabel.frame = CGRectMake(left + kImageWidth + kSmallMargin*2 , 
									   self.captionLabel.bottom, 
									   width - kImageWidth - right, 
									   self.titleLabel.font.lineHeight*2);
	self.titleLabel.backgroundColor = [UIColor clearColor];
	//Label Text
	if (self.detailTextLabel.text) {
		self.detailTextLabel.top = self.titleLabel.bottom;
		self.detailTextLabel.left = left + kSmallMargin;
		self.detailTextLabel.width = width - right;
	   [self.detailTextLabel sizeToFit];
	} else {
		self.detailTextLabel.frame = CGRectZero;
	}
	
//	self.captionLabel.backgroundColor = [UIColor greenColor];
//	self.detailTextLabel.backgroundColor = [UIColor redColor];
//	self.titleLabel.backgroundColor = [UIColor orangeColor];
//	_cityLabel.backgroundColor = [UIColor blueColor];

//	self.selectionStyle = UITableViewCellSelectionStyleBlue;
	self.accessoryType = UITableViewCellAccessoryNone;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTTableViewCell

- (id)object {
	return _item;
}



- (void)setObject:(id)object {
	if (_item != object) {
		[super setObject:object];
		
		TTTableMyNeedsItem* item = object;
		
		//DisplayName
		self.captionLabel.textColor = TTSTYLEVAR(needDisplayNameColor);
		self.captionLabel.font = TTSTYLEVAR(needDisplayNameFont);
		self.captionLabel.highlightedTextColor = TTSTYLEVAR(needTextColor);
		self.captionLabel.textAlignment = UITextAlignmentLeft;
		self.captionLabel.contentMode = UIViewContentModeTop;
		self.captionLabel.lineBreakMode = UILineBreakModeWordWrap;
		
		//Text
		self.detailTextLabel.textColor = TTSTYLEVAR(needTextColor);
		self.detailTextLabel.font = TTSTYLEVAR(needTextFont);
		self.detailTextLabel.adjustsFontSizeToFitWidth = YES;
		self.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
		self.detailTextLabel.textAlignment = UITextAlignmentLeft;
		self.detailTextLabel.contentMode = UIViewContentModeLeft;
		self.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
		self.detailTextLabel.numberOfLines = kMessageTextLineCount;
		
		//Subject
		self.titleLabel.textColor = TTSTYLEVAR(needSubjectColor);
		self.titleLabel.highlightedTextColor = TTSTYLEVAR(needTextColor);
		self.titleLabel.font = TTSTYLEVAR(needSubjectFont);
//		self.titleLabel.adjustsFontSizeToFitWidth = YES;		
		self.titleLabel.contentMode = UIViewContentModeTopLeft;
		self.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
//		self.titleLabel.numberOfLines = 0;
		
		//Date
		self.timestampLabel.font = TTSTYLEVAR(needNDateFont);
		self.timestampLabel.textColor = TTSTYLEVAR(needNDateColor);
		self.timestampLabel.highlightedTextColor = TTSTYLEVAR(needTextColor);		
		self.timestampLabel.contentMode = UIViewContentModeLeft;
		
		self.selectedBackgroundView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);
//		self.selectedBackgroundView.highlighted = true;
//		self.contentView.backgroundColor = TTSTYLEVAR(cellBackgroundColor);

//		
		_cityLabel.text = item.city;
		_cityLabel.font = TTSTYLEVAR(needCityFont);
		_cityLabel.textColor = TTSTYLEVAR(needCityColor);
		_cityLabel.highlightedTextColor = [UIColor whiteColor];
		_cityLabel.contentMode = UIViewContentModeRight;
		_cityLabel.textAlignment = UITextAlignmentRight;
		_cityLabel.backgroundColor = [UIColor clearColor];
		
		_helpsLabel.text = [NSString stringWithFormat:@"%@", item.helps];
		_helpsLabel.font = TTSTYLEVAR(needNDateFont);
		_helpsLabel.textColor = TTSTYLEVAR(needishBlueColor);
		_helpsLabel.highlightedTextColor = [UIColor whiteColor];
		_helpsLabel.contentMode = UIViewContentModeTop;
		_helpsLabel.textAlignment = UITextAlignmentRight;
		_helpsLabel.backgroundColor = [UIColor clearColor];
		
		
    }
}

@end
